<?php
/**
 * Created by PhpStorm.
 * User: 柳迪
 * Date: 2018/7/12
 * Time: 9:30
 */

namespace app\api\controller;


use think\Controller;
use think\Session;

class Match extends Controller
{
    public function getList()
    {
        $result=db('match')->field('mid,mname,mpic,maddtime,mstarttime,mintro,maddress,mlatitude,mlongitude,mprice')->order('mflag desc,maddtime desc')->select();
        echo json_encode($result);
        die();
    }

    public function getDetail()
    {
        if(input('?id'))
        {
            if(!db('match')->find(input('id')))
            {
                echo json_encode(['status'=>0,'message'=>'id错误。']);
                die();
            }
            $mid=input('id');
            $result=db('match')->field('mid,mname,mpic,maddtime,mstarttime,mintro,maddress,mlatitude,mlongitude,mprice')->find($mid);
            if(Session::has('uid'))
            {
                $result_find=db('enroll')->where(['uid'=>Session::get('uid'),'mid'=>$mid])->find();
                if ($result_find)
                    $result['flag']=1;
                else
                    $result['flag']=0;
            }
            else
            {
                $result['flag']=0;
            }
            echo json_encode($result);
            die();
        }
        else
        {
            echo json_encode(['status'=>0,'message'=>'参数错误']);
            die();
        }
    }

    public function enroll()
    {
        if(Session::has('uid'))
        {
            if(input('?id'))
            {
                $mid=input('id');
                $result=db('enroll')->where('mid',$mid)->where('uid',Session::get('uid'))->find();
                if($result)
                {
                    echo json_encode(['status'=>0,'message'=>'您已报名此项目，无法重复报名。']);
                    die();
                }
                $result_match=db('match')->field('mprice')->find($mid);
                if(!$result_match)
                {
                    echo json_encode(['status'=>0,'message'=>'参数id错误。']);
                    die();
                }
                $result_insert=db('enroll')->insert(['mid'=>$mid,'eprice'=>$result_match['mprice'],'uid'=>Session::get('uid'),'etime'=>date("Y-m-d H:i")]);
                if($result_insert)
                {
                    echo json_encode(['status'=>1,'message'=>'报名成功。']);
                    die();
                }
                else
                {
                    echo json_encode(['status'=>0,'message'=>'未知错误。']);
                    die();
                }
            }
        }
        else
        {
            echo json_encode(['status'=>0,'message'=>'尚未登陆，无法报名。']);
            die();
        }
    }

    public function cancel()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登陆。']);
            die();
        }
        if(input('?id'))
        {
            $mid=input('id');
            $uid=Session::get('uid');
            $data['uid']=$uid;
            $data['mid']=$mid;
            $result=db('enroll')->where($data)->find();
            if($result)
            {
                db('enroll')->delete($result['eid']);
                echo json_encode(['status'=>1,'message'=>'取消成功。']);
                die();
            }
            else
            {
                echo json_encode(['status'=>0,'message'=>'尚未报名。']);
                die();
            }
        }
    }

}